<?php

// phpMyRealty 3
//
// File Name: print.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

define('PMR', 'true');
include('./config.php');
include ( PATH . '/defaults.php');

// Check the id variable
if (isset($_GET['id']) && eregi('^[0-9]+$', $_GET['id']))

 {

  $sql = 'SELECT title FROM ' . PROPERTIES_TABLE  . ' WHERE approved = 1 AND id = "' . safehtml($_GET['id']) . '" LIMIT 1';
  $r_title = $db->query ( $sql );
  $f_title = $db->fetcharray ( $r_title );

 }

// Title tag content
$title = $conf['website_name_short'] . ' - ' . $f_title['title'];

// Template header
include ( PATH . '/templates/' . $cookie_template . '/header-print.php' );

// Check the id variable
if (isset($_GET['id']) && eregi('^[0-9]+$', $_GET['id']))

 {

  echo table_header ( $lang['Information'] );

  // Update HITS counter

  $sql = 'UPDATE ' . PROPERTIES_TABLE  . ' SET hits = hits+1 WHERE approved = 1 AND id = "' . safehtml($_GET['id']) . '" LIMIT 1';

  if ( $session->fetch('listing_counter_id') != $_GET['id'] 
  || (time() - $session->fetch('listing_counter_time')) > 60*60*24 )
   {
    $db->query ( $sql ) or error ('Critical Error' , mysql_error());
   }
  // Cache the last listing id not to allow this user
  // to be counted next time he visit this listing
  // This variable stores id and time to allow the counter
  // be updated in 24 hours even if this user did not visit
  // any other listing to empty the cached data
  $session->set('listing_counter_id', intval($_GET['id']));
  $session->set('listing_counter_time', time());

  //

	// Title/descr language to use (if available)
	$title = str_replace( 'name', 'title', $language_in );
	$description = str_replace( 'name', 'description', $language_in );

  $sql = 'SELECT ' . $title . ', ' . $description . ', ' . PROPERTIES_TABLE . '.* FROM ' . PROPERTIES_TABLE  . ' WHERE approved = 1 AND id = "' . safehtml($_GET['id']) . '" LIMIT 1';
  $r = $db->query ( $sql ) or error ('Critical Error' , mysql_error());

  echo '
   <table width="100%" cellpadding="5" cellspacing="0" border="0">
       ';

  $f = $db->fetcharray( $r );


  // Starting a new template
  $template = new Template;

  $tpl = implode ('', file( PATH . '/templates/' . $cookie_template . '/tpl/property_detailed_print.tpl' ));

  // Load listing short search results template
  $template->load ( $tpl );

  // Make all values in the array 'n/a' if empty
  $f = array_map ( 'if_empty', $f);

  // Replace the template variables

  $sql = 'SELECT * FROM ' . USERS_TABLE  . ' WHERE id = "' . $f['userid'] . '" LIMIT 1';
  $r_user = $db->query ( $sql );
  $f_user = $db->fetcharray ( $r_user );

  if ($f_user['package'] != '0' && $f_user['package'] != '')
   {

    $sql = 'SELECT * FROM ' . PACKAGES_AGENT_TABLE  . ' WHERE id = "' . $f_user['package'] . '" LIMIT 1';
    $r_package = $db->query ( $sql );
    $f_package = $db->fetcharray ( $r_package );

   }

  else

   {

    $f_package['listings'] = $conf['free_listings'];
    $f_package['gallery'] = $conf['free_gallery'];
    $f_package['mainimage'] = $conf['free_mainimage'];
    $f_package['photo'] = $conf['free_photo'];
    $f_package['phone'] = $conf['free_phone'];
    $f_package['address'] = $conf['free_address'];
    
   }
   
	// Default
	if ($f[0] == '')
		$f['title'] = $f['title'];
	else
		$f['title'] = $f[0];
		
	if ($f[1] == '')
		$f['description'] = $f['description'];
	else
		$f['description'] = $f[1];

  if ($f_package['mainimage'] == 'ON')
   $template->set ( 'image', show_gallery_image ('images', $f['id']) );
  else
   $template->set ( 'image', '' );

  $template->set ( 'status', getnamebyid ( STATUS_TABLE, $f['status'] ) );
  $template->set ( 'mls', $f['mls'] );
  $template->set ( 'title', $f['title'] );
  $template->set ( 'type', getnamebyid ( TYPES_TABLE, $f['type'] ) );
  $template->set ( 'type2', getnamebyid ( TYPES2_TABLE, $f['type2'] ) );
  $template->set ( 'style', getnamebyid ( STYLES_TABLE, $f['style'] ) );
  $template->set ( 'description', unsafehtml($f['description']) );
  $template->set ( 'lot_size', $f['size'] );
  $template->set ( 'dimensions', $f['dimensions'] );
  $template->set ( 'bathrooms', $f['bathrooms'] );
  $template->set ( 'half_bathrooms', $f['half_bathrooms'] );
  $template->set ( 'bedrooms', $f['bedrooms'] );

  $cat = explode ("#", $f['location2']);
  $r11 = $db->query('SELECT category FROM ' . LOCATION1_TABLE . ' WHERE selector = "' . $cat[0] . '"');
  $f11 = $db->fetcharray($r11);
  $template->set ( 'location1', $f11['category'] );

  $r22 = $db->query('SELECT subcategory FROM ' . LOCATION2_TABLE . ' WHERE catsubsel = "' . $cat[1] . '"');
  $f22 = $db->fetcharray($r22);
  $template->set ( 'location2', $f22['subcategory'] );

  $r33 = $db->query('SELECT subsubcategory FROM ' . LOCATION3_TABLE . ' WHERE catsubsubsel = "' . $cat[2] . '"');
  $f33 = $db->fetcharray($r33);
  $template->set ( 'location3', $f33['subsubcategory'] );

  if ($f['display_address'] == 'YES' &&  $f_package['address'] == 'ON')
   {
    $template->set ( 'address1', $f['address1'] );
    $template->set ( 'address2', $f['address2'] );
    $template->set ( 'zip', $f['zip'] );
   }
  elseif ($f['display_address'] != 'YES' || $f_package['address'] != 'ON')
   {
    $template->set ( 'address1', ' ' );
    $template->set ( 'address2', ' ' );
    $template->set ( 'zip', ' ' );
   }

  $template->set ( 'price', pmr_number_format($f['price']) );
  $template->set ( 'currency', $conf['currency'] );

  // DIRECTIONS

  $template->set ( 'directions', $f['directions']);

  unset($directions);

  // DIRECTIONS : END

  $template->set ( 'year_built', $f['year_built'] );
  $template->set ( 'buildings', show_multiple ( BUILDINGS_TABLE, $f['buildings'] ) );
  $template->set ( 'appliances', show_multiple ( APPLIANCES_TABLE, $f['appliances'] ) );
  $template->set ( 'features', show_multiple ( FEATURES_TABLE, $f['features'] ) );
  $template->set ( 'garage', getnamebyid ( GARAGE_TABLE, $f['garage'] ) );
  $template->set ( 'garage_cars', $f['garage_cars'] );
  $template->set ( 'basement', getnamebyid ( BASEMENT_TABLE, $f['basement'] ) );
  $template->set ( 'date_added', printdate($f['date_added']) );
  $template->set ( 'date_updated', printdate($f['date_updated']) );
  $template->set ( 'date_upgraded', printdate($f['date_upgraded']) );
  $template->set ( 'ip_added', $f['ip_added'] );
  $template->set ( 'ip_updated', $f['ip_updated'] );
  $template->set ( 'ip_upgraded', $f['ip_upgraded'] );
  $template->set ( 'hits', $f['hits'] );
  
	// Custom fields
	
	// Check if custom field is input, if it is, it will have {INPUT}{value} in the database, otherwise, it will have a primary key reference
	$custom_fields = array(
						'custom1' => $f['custom1'],
						'custom2' => $f['custom2'],
						'custom3' => $f['custom3'],
						'custom4' => $f['custom4'],
						'custom5' => $f['custom5'],
						'custom6' => $f['custom6'],
						'custom7' => $f['custom7'],
						'custom8' => $f['custom8'],
						'custom9' => $f['custom9'],
						'custom10' => $f['custom10'],
	);
	foreach ( $custom_fields AS $key => $value )
	{
		if ( strpos( $value, '{INPUT}' ) !== false )
		{
			// Input box
			$f[$key] = str_replace( '{INPUT}', '', $value );
		}
		else
		{
			// Select box
			$f[$key] = show_custom_value ( $db, $f[$key],  VALUES_TABLE );
		}
	}

	// Custom fields
	$template->set ( '@custom1', show_custom_value ( $db, 'custom1', FIELDS_TABLE ) );
	$template->set ( '@custom2', show_custom_value ( $db, 'custom2', FIELDS_TABLE ) );
	$template->set ( '@custom3', show_custom_value ( $db, 'custom3', FIELDS_TABLE ) );
	$template->set ( '@custom4', show_custom_value ( $db, 'custom4', FIELDS_TABLE ) );
	$template->set ( '@custom5', show_custom_value ( $db, 'custom5', FIELDS_TABLE ) );
	$template->set ( '@custom6', show_custom_value ( $db, 'custom6', FIELDS_TABLE ) );
	$template->set ( '@custom7', show_custom_value ( $db, 'custom7', FIELDS_TABLE ) );
	$template->set ( '@custom8', show_custom_value ( $db, 'custom8', FIELDS_TABLE ) );
	$template->set ( '@custom9', show_custom_value ( $db, 'custom9', FIELDS_TABLE ) );
	$template->set ( '@custom10', show_custom_value ( $db, 'custom10', FIELDS_TABLE ) );

	$template->set ( 'custom1', $f['custom1'] );
	$template->set ( 'custom2', $f['custom2'] );
	$template->set ( 'custom3', $f['custom3'] );
	$template->set ( 'custom4', $f['custom4'] );
	$template->set ( 'custom5', $f['custom5'] );
	$template->set ( 'custom6', $f['custom6'] );
	$template->set ( 'custom7', $f['custom7'] );
	$template->set ( 'custom8', $f['custom8'] );
	$template->set ( 'custom9', $f['custom9'] );
	$template->set ( 'custom10', $f['custom10'] );

  // Will open the map in the new window
  $template->set ( 'map' , '');

  // MAP : END

  $template->set ( 'slideshow' , '');

  // Defaults for their account type
  $paid_account = false;
  $account = false;
  $agent_details = '';
  
  	// If they have an account
  	if (auth_check($session->fetch('login'), $session->fetch('password')))
  	{
		  // Fetching the user ID from the user's table
		  $sql = 'SELECT approved, id, package FROM ' . USERS_TABLE . ' WHERE login = "' . $session->fetch('login') . '" LIMIT 1';
		  $r = $db->query( $sql );
		  $f = $db->fetcharray( $r );
		  
		  $account = true;
		  
		  if ($f['package'] > 0)
		  	$paid_account = true;
		  else
		  	$paid_account = false;
  	}
  	else
  	{
  		$paid_account = false;
  		$account = false;
  	}
  
	// Agent details (whether this displays is configurable via admin panel > configuration settings)
	
	// If anyone can view agent details or registered only but they are logged in or paid only but they are paid
	if ($conf['contact_agents'] == '1' || $paid_account == true || ($conf['contact_agents'] == '2' && $account == true))
	{
	
		if ($conf['rewrite'] == 'ON')
			$template->set ( 'view_realtor', '<a href="' . URL . '/Realtor/' . $f['userid'] . '.html">' . $f_user['first_name'] . ' ' . $f_user['last_name'] . '</a>'  );
		else
			$template->set ( 'view_realtor', '<a href="' . URL . '/viewuser.php?id=' . $f['userid'] . '">' . $f_user['first_name'] . ' ' . $f_user['last_name'] . '</a>');

		$template->set ( 'realtor_first_name', $f_user['first_name'] );
		$template->set ( 'realtor_last_name', $f_user['last_name'] );
		$template->set ( 'realtor_company_name', $f_user['company_name'] );
		
		if ($f_package['phone'] == 'ON')
		{
			$template->set ( 'realtor_phone', $f_user['phone'] );
			$template->set ( 'realtor_fax', $f_user['fax'] );
			$template->set ( 'realtor_mobile', $f_user['mobile'] );
		}
		else
		{
			$template->set ( 'realtor_phone', '' );
			$template->set ( 'realtor_fax', '' );
			$template->set ( 'realtor_mobile', '' );
		}
	}
		else
	{
	
		// Determine message
		if ($conf['contact_agents'] == '2' && $account == false)
		{
			$agent_details = $lang['Agent_Details_Account_Only'];
		}
		// Paid account only, but they don't have a paid account
		elseif ($conf['contact_agents'] == '3' && $paid_account == false)
		{
			$agent_details = $lang['Agent_Details_Pay_Only'];
		}
	
		$template->set ( 'realtor_first_name', '' );
		$template->set ( 'realtor_last_name', '' );
		$template->set ( 'realtor_company_name', '' );
		$template->set ( 'realtor_phone', '' );
		$template->set ( 'realtor_fax', '' );
		$template->set ( 'realtor_mobile', '' );
		$template->set ( 'view_realtor', $agent_details );
	}

  $template->set ( 'realtor_mail', '' );
  $template->set ( 'realtor_website', '' );

  $template->set ( 'new', newitem ( PROPERTIES_TABLE, $f['id'], $conf['new_days']) );
  $template->set ( 'updated', updateditem ( PROPERTIES_TABLE, $f['id'], $conf['updated_days']) );
  $template->set ( 'featured', featureditem ( $f['featured'] ) );

  // Names of the variables

  $template->set ( '@status', $lang['Listing_Status'] );
  $template->set ( '@mls', $lang['Listing_MLS'] );
  $template->set ( '@title', $lang['Listing_Title'] );
  $template->set ( '@type', $lang['Listing_Property_Type'] );
  $template->set ( '@type2', $lang['Module_Listing_Type'] );
  $template->set ( '@style', $lang['Listing_Style'] );
  $template->set ( '@description', $lang['Listing_Description'] );
  $template->set ( '@lot_size', $lang['Listing_Lot_Size'] );
  $template->set ( '@dimensions', $lang['Listing_Dimensions'] );
  $template->set ( '@bathrooms', $lang['Listing_Bathrooms'] );
  $template->set ( '@half_bathrooms', $lang['Listing_Half_Bathrooms'] );
  $template->set ( '@bedrooms', $lang['Listing_Bedrooms'] );
  $template->set ( '@location', $lang['Search_Location'] );
  $template->set ( '@city', $lang['City'] );
  $template->set ( '@address1', $lang['Listing_Address1'] );
  $template->set ( '@address2', $lang['Listing_Address2'] );
  $template->set ( '@zip', $lang['Zip_Code'] );
  $template->set ( '@price', $lang['Listing_Price'] );
  $template->set ( '@directions', $lang['Listing_Directions'] );
  $template->set ( '@year_built', $lang['Listing_Year_Built'] );
  $template->set ( '@buildings', $lang['Listing_Additional_Out_Buildings'] );
  $template->set ( '@appliances', $lang['Listing_Appliances_Included'] );
  $template->set ( '@features', $lang['Listing_Features'] );
  $template->set ( '@garage', $lang['Listing_Garage'] );
  $template->set ( '@garage_cars', $lang['Listing_Garage_Cars'] );
  $template->set ( '@basement', $lang['Listing_Basement'] );
  $template->set ( '@date_added', $lang['Date_Added'] );
  $template->set ( '@date_updated', $lang['Date_Updated'] );
  $template->set ( '@date_upgraded', $lang['Date_Upgraded'] );
  $template->set ( '@hits', $lang['Hits'] );
  $template->set ( '@view_realtor', $lang['View_Realtor'] );

  $template->set ( '@image_url', URL . '/templates/' . $cookie_template . '/images' );

  // Publish template
  $template->publish();


    echo '
     </table>
         ';
    echo table_footer ();

 }

else

 echo 'Sorry, ID is not specified';

// Template footer
include ( PATH . '/templates/' . $cookie_template . '/footer-print.php' );

?>